using System;
using System.ComponentModel;
using System.Security.Principal;
using System.Web;
using System.Web.SessionState;

namespace WebMonitor.Http.Impl {
    public class HttpApplicationAdapter : IHttpApplication {
        private readonly HttpApplication app;

        public HttpApplicationAdapter(HttpApplication app) {
            this.app = app;
        }

        public void CompleteRequest() {
            app.CompleteRequest();
        }

        public void AddOnBeginRequestAsync(BeginEventHandler bh, EndEventHandler eh) {
            app.AddOnBeginRequestAsync(bh, eh);
        }

        public void AddOnAuthenticateRequestAsync(BeginEventHandler bh, EndEventHandler eh) {
            app.AddOnAuthenticateRequestAsync(bh, eh);
        }

        public void AddOnAuthorizeRequestAsync(BeginEventHandler bh, EndEventHandler eh) {
            app.AddOnAuthorizeRequestAsync(bh, eh);
        }

        public void AddOnResolveRequestCacheAsync(BeginEventHandler bh, EndEventHandler eh) {
            app.AddOnResolveRequestCacheAsync(bh, eh);
        }

        public event EventHandler AcquireRequestState {
            add { app.AcquireRequestState += value; }
            remove { app.AcquireRequestState -= value; }
        }

        public event EventHandler AuthenticateRequest {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }

        public event EventHandler AuthorizeRequest {
            add { app.AuthorizeRequest += value; }
            remove { app.AuthorizeRequest -= value; }
        }

        public event EventHandler BeginRequest {
            add { app.BeginRequest += value; }
            remove { app.BeginRequest -= value; }
        }

        public event EventHandler EndRequest {
            add { app.EndRequest += value; }
            remove { app.EndRequest -= value; }
        }

        public event EventHandler Error {
            add { app.Error += value; }
            remove { app.Error -= value; }
        }

        public event EventHandler PostRequestHandlerExecute {
            add { app.PostRequestHandlerExecute += value; }
            remove { app.PostRequestHandlerExecute -= value; }
        }

        public event EventHandler PreRequestHandlerExecute {
            add { app.PreRequestHandlerExecute += value; }
            remove { app.PreRequestHandlerExecute -= value; }
        }

        public event EventHandler PreSendRequestContent {
            add { app.PreSendRequestContent += value; }
            remove { app.PreSendRequestContent -= value; }
        }

        public event EventHandler PreSendRequestHeaders {
            add { app.PreSendRequestHeaders += value; }
            remove { app.PreSendRequestHeaders -= value; }
        }

        public event EventHandler ReleaseRequestState {
            add { app.ReleaseRequestState += value; }
            remove { app.ReleaseRequestState -= value; }
        }

        public event EventHandler ResolveRequestCache {
            add { app.ResolveRequestCache += value; }
            remove { app.ResolveRequestCache -= value; }
        }

        public event EventHandler UpdateRequestCache {
            add { app.UpdateRequestCache += value; }
            remove { app.UpdateRequestCache -= value; }
        }

        public void AddOnAcquireRequestStateAsync(BeginEventHandler bh, EndEventHandler eh) {
            app.AddOnAcquireRequestStateAsync(bh, eh);
        }

        public void AddOnPreRequestHandlerExecuteAsync(BeginEventHandler bh, EndEventHandler eh) {
            app.AddOnPreRequestHandlerExecuteAsync(bh, eh);
        }

        public void AddOnPostRequestHandlerExecuteAsync(BeginEventHandler bh, EndEventHandler eh) {
            app.AddOnPostRequestHandlerExecuteAsync(bh, eh);
        }

        public void AddOnReleaseRequestStateAsync(BeginEventHandler bh, EndEventHandler eh) {
            app.AddOnReleaseRequestStateAsync(bh, eh);
        }

        public void AddOnUpdateRequestCacheAsync(BeginEventHandler bh, EndEventHandler eh) {
            app.AddOnUpdateRequestCacheAsync(bh, eh);
        }

        public void AddOnEndRequestAsync(BeginEventHandler bh, EndEventHandler eh) {
            app.AddOnEndRequestAsync(bh, eh);
        }

        public void Init() {
            app.Init();
        }

        public void Dispose() {
            app.Dispose();
        }

        public string GetVaryByCustomString(HttpContext context, string custom) {
            return app.GetVaryByCustomString(context, custom);
        }

        public IHttpContext Context {
            get { return new HttpContextAdapter(app.Context); }
        }

        public IHttpRequest Request {
            get { return new HttpRequestAdapter(app.Request); }
        }

        public IHttpResponse Response {
            get { return new HttpResponseAdapter(app.Response); }
        }

        public IHttpSessionState Session {
            get { return new HttpSesssionStateAdapter(app.Session); }
        }

        public IHttpApplicationState Application {
            get { return new HttpApplicationStateAdapter(app.Application); }
        }

        public IHttpServerUtility Server {
            get { return new HttpServerUtilityAdapter(app.Server); }
        }

        public IPrincipal User {
            get { return app.User; }
        }

        public HttpModuleCollection Modules {
            get { return app.Modules; }
        }

        public event EventHandler Disposed {
            add { app.Disposed += value; }
            remove { app.Disposed -= value; }
        }

        public ISite Site {
            get { return app.Site; }
            set { app.Site = value; }
        }

        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) {
            return ((IHttpAsyncHandler) app).BeginProcessRequest(context, cb, extraData);
        }

        public void EndProcessRequest(IAsyncResult result) {
            ((IHttpAsyncHandler) app).EndProcessRequest(result);
        }

        public void ProcessRequest(HttpContext context) {
            ((IHttpHandler) app).ProcessRequest(context);
        }

        public bool IsReusable {
            get { return ((IHttpHandler) app).IsReusable; }
        }
    }
}